<template>
    <div>
        <el-form :inline="true" :model="searchForm" class="demo-form-inline">


            <el-form-item>

                <el-button type="primary" @click="openAddQuestion">新建评估</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="questionList" v-if="questionList.length">
            <el-table-column type="expand">
                <template slot-scope="props">

                    <div v-for="item in props.row.options" :key="item.id" style="margin:10px 50px;">
                        <el-tag :type="item.id == props.row.answerId ? 'success' : 'info'"
                            :effect="item.id == props.row.answerId ? 'light' : 'plain'">
                            {{ item.label + " " + item.text }}
                        </el-tag>
                    </div>



                </template>
            </el-table-column>
            <el-table-column prop="id" label="id" width="140">
            </el-table-column>
            <el-table-column prop="type" label="类型" width="120">
            </el-table-column>
            <el-table-column prop="question" label="题目">
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="200">
                <template slot-scope="scope">
                    <el-button @click="openDetail(scope.row.id)" type="text">查看结果</el-button>
                    <el-button @click="deleteQue(scope.row.id)" type="text">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-empty description="暂无数据" v-else></el-empty>
        <div class="pagination"> <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="searchForm.page" :page-sizes="[10, 20, 50, 100]" :page-size="searchForm.size"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination></div>


        <el-dialog title="新建评估" :visible.sync="dialogFormVisible">
            <el-form :model="form" ref="form">
                <el-form-item label="选择组合:" label-width="140px">
                    <el-select v-model="form.typeId" placeholder="请选择组合">
                        <el-option v-for="item in typeOption" :key="item.id" :label="item.name"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="选择模型" label-width="140px">
                    <el-select v-model="form.typeId" placeholder="请选择模型">
                        <el-option v-for="item in typeOption" :key="item.id" :label="item.name"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="模型参数:" label-width="140px">
                    <el-input v-model="form.question"></el-input>
                </el-form-item>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addQuestion">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="结果" :visible.sync="dialogDetailVisible" width="400px">
            <el-form :model="detailForm" ref="form">
                <el-form-item label="组合名:" label-width="140px">
                    {{ detailForm.team }}
                </el-form-item>
                <el-form-item label="模型:" label-width="140px">
                    {{ detailForm.team }}
                </el-form-item> <el-form-item label="日期:" label-width="140px">
                    {{ detailForm.team }}
                </el-form-item> <el-form-item label="评估参数:" label-width="140px">
                    {{ detailForm.team }}
                </el-form-item> <el-form-item label="结果:" label-width="140px">
                    {{ detailForm.team }}
                </el-form-item> <el-form-item label="置信度:" label-width="140px">
                    {{ detailForm.team }}
                </el-form-item>



            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogDetailVisible = false">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getAllQuestion, getQuestionType, addQuestion, deleteQuestion } from "@/api/question"
export default {
    name: 'HelloWorld',
    props: {
        msg: String
    },
    data() {
        return {
            questionList: [],
            total: 100,
            totalPage: 0,

            searchForm: {
                typeId: "",
                page: 1,
                size: 10
            },
            form: {
                "typeId": 1,
                "question": "请问你是？",
                "options": [
                    {
                        "label": "A",
                        "text": "answer A",
                        "isAnswer": false
                    },
                    {
                        "label": "B",
                        "text": "answer B",
                        "isAnswer": true
                    }, {
                        "label": "C",
                        "text": "answer C",
                        "isAnswer": false
                    }, {
                        "label": "D",
                        "text": "answer D",
                        "isAnswer": false
                    }
                ]
            },
            detailForm: {team:"test"},
            formAnswer: "",
            typeOption: [],
            dialogFormVisible: false,
            dialogDetailVisible: false
        }
    },
    created() {
        this.getQuestion(this.searchForm);
        this.getQuestionType();
    },
    methods: {
        async getQuestion(form) {
            await getAllQuestion(form).then(res => {

                this.questionList = res.data.data.contentList
                this.total = res.data.data.totalNum
                this.totalPage = res.data.data.totalPage
                // form = { typeId: "" }
            }).catch(error => {
                console.log(error)
            })
        },
        getQuestionType() {
            getQuestionType().then(res => {

                this.typeOption = res.data.data.types

            }).catch(error => {
                console.log(error)
            })
        },
        searchSubmit() {
            this.searchForm.page = 1;
            this.getQuestion(this.searchForm);
        },
        openDetail() {
            this.dialogDetailVisible = true;

        },
        openAddQuestion() {
            this.dialogFormVisible = true;
            this.resetLoginForm();
        },
        deleteQue(id) {
            this.$confirm('确定删除该题目?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteQuestion(id).then(async () => {
                    await this.getQuestion(this.searchForm);
                    if (this.searchForm.page > this.totalPage) {
                        this.searchForm.page = this.totalPage;
                        this.getQuestion(this.searchForm);
                    }
                    // this.$message({
                    //     type: 'success',
                    //     message: '删除成功!'
                    // });
                })

            }).catch(() => {
                // this.$message({
                //     type: 'info',
                //     message: '已取消删除'
                // });
            });



        },
        resetLoginForm() {
            this.form = {
                "typeId": "",
                "question": "",
                "options": [
                    {
                        "label": "A",
                        "text": "",
                        "isAnswer": false
                    },
                    {
                        "label": "B",
                        "text": "",
                        "isAnswer": true
                    }, {
                        "label": "C",
                        "text": "",
                        "isAnswer": false
                    }, {
                        "label": "D",
                        "text": "",
                        "isAnswer": false
                    }
                ]
            }
        },
        addQuestion() {
            this.dialogFormVisible = false;
            this.form.options.forEach((el, index) => {
                if (index == this.formAnswer) el.isAnswer = true;
                else el.isAnswer = false;
            });
            addQuestion(this.form).then(async () => {
                this.dialogFormVisible = false;
                await this.getQuestion(this.searchForm);
                if (this.searchForm.page != this.totalPage) {
                    this.searchForm.page = this.totalPage;
                    this.getQuestion(this.searchForm);
                }
            })
        },
        handleSizeChange(val) {
            this.searchForm.size = val
            this.getQuestion(this.searchForm);
            console.log(`每页 ${val} 条`);
        },
        handleCurrentChange(val) {
            this.searchForm.page = val
            this.getQuestion(this.searchForm);

            console.log(`当前页: ${val}`);
        }
    }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h3 {
    margin: 40px 0 0;
}

ul {
    list-style-type: none;
    padding: 0;
}

li {
    display: inline-block;
    margin: 0 10px;
}

a {
    color: #42b983;
}

.pagination {
    margin-top: 10px;
    float: right;
}
</style>